<!--
 * @Description: 平台标准任务黑名单
 * @Author: liyujie
 * @Date: 2021/10/27
 * @Last Modified by: liyujie
 * @Last Modified time: 2021/10/27
-->
<template>
    <PageLayout
        title="平台标准任务黑名单"
        class="standard-task-blacklist-wrapper"
    >
        <template slot="filter">
            <zd-tabs
                :tab-list="tabsList"
                v-model="filterData.task_category"
                style="padding-left: 8px"
                @change="handleTabChange()"
            />
            <div class="filter-view">
                <div class="left">
                    <div class="filter-item">
                        <div class="filter-item__title">
                            用户姓名/编号/电话
                        </div>
                        <el-input
                            placeholder="搜索用户姓名/编号/电话"
                            v-model="filterData.keyword"
                            suffix-icon="el-icon-search"
                            clearable
                            size="medium"
                            style="width: 240px"
                            @keydown.enter.native="getTableData(1)"
                            @change="getTableData(1)"
                        />
                    </div>
                    <div class="filter-item">
                        <el-button
                            size="medium"
                            class="query-button"
                            @click="getTableData(1)"
                        >
                            查询
                        </el-button>
                    </div>
                </div>
                <div class="right">
                    <el-button
                        type="primary"
                        size="medium"
                        icon="el-icon-plus"
                        @click="showAddDialog"
                    >
                        添加用户
                    </el-button>
                </div>
            </div>
        </template>
        <el-table
            :data="tableData"
            height="100%"
            style="width: 100%;"
            ref="table"
            class="data-table"
            v-loading="tableLoading"
        >
            <el-table-column
                label="序号"
                align="center"
                width="80"
                type="index"
            />
            <el-table-column
                prop="true_name"
                label="用户昵称"
                show-overflow-tooltip
                align="left"
                :formatter="emptyFormatter"
            />
            <el-table-column
                prop="student_no"
                label="用户编号"
                show-overflow-tooltip
                :formatter="emptyFormatter"
            />
            <el-table-column
                prop="school_name"
                label="所在学校"
                show-overflow-tooltip
                :formatter="emptyFormatter"
            />
            <el-table-column
                prop="phone"
                label="手机号码"
                show-overflow-tooltip
                :formatter="emptyFormatter"
            />
            <el-table-column
                prop="invite_code"
                label="邀请码"
                show-overflow-tooltip
                :formatter="emptyFormatter"
            />
            <el-table-column
                label="操作"
                align="left"
                width="140"
            >
                <template slot-scope="scope">
                    <div class="option-btn-group">
                        <el-button
                            type="text"
                            size="medium"
                            @click="setBlacklist(scope.row)"
                        >
                            移出黑名单
                        </el-button>
                    </div>
                </template>
            </el-table-column>
        </el-table>
        <template slot="pagination">
            <div/>
            <el-pagination
                layout="total, prev, pager, next, jumper"
                background
                :current-page.sync="page.now_page"
                :total="page.total_count"
                :page-size="page.page_size"
                @current-change="getTableData"
            />
        </template>
        <el-dialog
            title="添加用户"
            :visible.sync="dialogVisible"
            width="680px"
            :close-on-click-modal="false"
            @close="handleClose"
        >
            <div>
                <!-- 筛选-->
                <div class="filter-wrap">
                    <div class="header-new-box">
                        <p class="header-new-box-title">用户昵称/ID/邀请码：</p>
                        <el-input
                            class="input-with-select"
                            placeholder="请输入用户昵称/ID/邀请码"
                            v-model="userFilter.keyword"
                            clearable
                            size="small"
                            @keydown.enter.native="getUserTableData(1)"
                        />
                    </div>
                    <el-button
                        type="primary"
                        size="small"
                        class="search-btn"
                        @click="getUserTableData(1)"
                    >
                        搜索
                    </el-button>
                </div>
                <!-- table -->
                <el-table
                    :data="userTableData"
                    max-height="438"
                    style="width: 100%;"
                    class="data-table"
                    v-if="userTableData.length"
                >
                    <el-table-column
                        prop="true_name"
                        label="用户名"
                        align="center"
                        show-overflow-tooltip
                    />
                    <el-table-column
                        prop="phone"
                        label="用户手机号"
                        align="center"
                    />
                    <el-table-column
                        prop="student_no"
                        label="用户编号"
                        align="center"
                        show-overflow-tooltip
                    />
                    <el-table-column
                        prop="invite_code"
                        label="邀请码"
                        align="center"
                        show-overflow-tooltip
                    />
                    <el-table-column
                        label="操作"
                        align="center"
                    >
                        <template slot-scope="scope">
                            <el-button
                                type="text"
                                @click="handleSelect(scope.row)"
                            >
                                选择
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <!-- /分页 -->
                <div
                    class="pagination-wrapper clearfix"
                    style="text-align: right;margin-top: 24px"
                    v-if="userTableData.length"
                >
                    <el-pagination
                        layout="total, prev, pager, next, jumper"
                        background
                        :current-page.sync="userPage.now_page"
                        :total="userPage.total_count"
                        :page-size="userPage.page_size"
                        @current-change="getTableData"
                    />
                </div>
            </div>
        </el-dialog>
    </PageLayout>
</template>

<script>
import { back } from '@/mixin/back';
import ZdTabs from "@/components/zd-tabs";

export default {
    name: 'StandardTaskBlacklist',
    mixins: [back],
    components: {ZdTabs},
    data() {
        return {
            filterData: {
                task_category: 7,
                keyword: '',
            },
            tabsList: [
                {label: '推广引流', value: 7},
                {label: '问卷调研', value: 8},
            ],
            userFilter: {
                keyword: ''
            },
            tableData: [],
            tableLoading: false,
            userTableData: [],
            userPage: {
                now_page: 1,
                total_count: 0,
                page_size: 20
            },
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 20
            },
            dialogVisible: false,
            authResult: {}
        };
    },
    computed: {},
    watch: {},
    filters: {
        statusFilter(val) {
            switch (val) {
                case -1:
                    return '失败';
                case 0:
                    return '进行中';
                case 1:
                    return '成功';
                default:
                    return '--';
            }
        },
    },
    mounted() {
        this.$store.commit('SET_HEADER', [{title: "APP后台"}, {title: '工作'}, {title: '平台标准任务'},{title: '黑名单'}]);
        this.authResult = this.$route.meta.authResult;
        this.getTableData();
    },

    methods: {

        /**
         * @description 获取列表数据
         * @param page
         */
        getTableData(page) {
            this.tableLoading = true;
            let params = { ...this.filterData };

            params.page_id = page || this.page.now_page;
            this.$post('/student/task_stuagent%5Cstudent_block%5Cget_list', params, res => {
                if (res.code === 1) {
                    this.tableData = res.data.list;
                    this.page = res.data.page;
                } else {
                    this.$notify({
                        title: '提示',
                        message: res.msg,
                        type: 'warning'
                    });
                }
                this.tableLoading = false;
            });
        },

        /**
         * 路由跳转
         * @param routeName
         * @param query
         */
        handleRoute(routeName, query) {
            this.$router.push({
                name: routeName,
                query
            });
        },

        /**
         * @description 过滤函数
         */
        emptyFormatter(row, column, val) {
            if (!val) return '--';
            return val;
        },

        /**
         * 移除黑名单
         * @param row
         */
        setBlacklist(row) {
            if(!this.authResult.auth_remove) {
                this.$notify({
                    title: '提示',
                    message: '请联系管理员添加权限',
                    type: 'warning',
                    duration: 2000
                });
                return false;
            }
            this.$zdConfirm(`确认要移出黑名单吗?`, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                let params = {
                    id: row.id
                };

                this.$post('/student/task_stuagent%5Cstudent_block%5Cdelete', params, resp => {
                    if (resp.code === 1) {
                        this.getTableData();
                        this.$notify({
                            title: '提示',
                            message: '操作成功',
                            duration: 2000,
                            type: 'success'
                        });
                    } else {
                        this.$notify({
                            title: '提示',
                            message: resp.msg,
                            duration: 2000,
                            type: 'warning'
                        });
                    }
                });
            }).catch(() => {});
        },

        /**
         * 获取学生列表
         */
        getUserTableData() {
            let params = {...this.userFilter};

            this.$post('/student/task_stuagent%5Cstudent_block%5Cget_student_list_for_select', params, resp => {
                if (resp.code === 1) {
                    this.userTableData = resp.data.list;
                    this.userPage = resp.data.page;
                    if(resp.data.list.length === 0) {
                        this.$message.warning('查询结果为空');
                    }
                } else {
                    this.$notify({
                        title: '提示',
                        message: resp.msg,
                        duration: 2000,
                        type: 'warning'
                    });
                }
            });
        },

        /**
         * 弹窗关闭
         */
        handleClose() {
            this.userFilter.keyword = '';
            this.userTableData = [];
        },

        /**
         * 选择
         * @param row
         */
        handleSelect(row) {
            this.$zdConfirm(`确定将用户${row.true_name}加入黑名单吗?`, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$post('/student/task_stuagent%5Cstudent_block%5Cadd', {
                    student_no: row.student_no,
                    task_category: this.filterData.task_category
                }, resp => {
                    if (resp.code === 1) {
                        this.dialogVisible = false;
                        this.getTableData();
                        this.$notify({
                            title: '提示',
                            message: '操作成功',
                            duration: 2000,
                            type: 'success'
                        });
                    } else {
                        this.$notify({
                            title: '提示',
                            message: resp.msg,
                            duration: 2000,
                            type: 'warning'
                        });
                    }
                });
            }).catch(() => {});
        },

        /**
         * 添加用户
         */
        showAddDialog() {
            if(!this.authResult.auth_add) {
                this.$notify({
                    title: '提示',
                    message: '请联系管理员添加权限',
                    type: 'warning',
                    duration: 2000
                });
                return false;
            }
            this.dialogVisible = true;
        },

        /**
         * tab切换
         */
        handleTabChange() {
            this.getTableData(1);
            this.$nextTick(() => {
                this.$refs.table.doLayout();
            });
        },
    }
};
</script>

<style lang="less" scoped>

.standard-task-blacklist-wrapper {
    /deep/ .content-wrapper {
        .header {
            display: none;
        }

        .filter-wrapper {
            display: initial !important;
            margin-top: 0 !important;
            padding: 0 !important;

            .filter-view {
                display: flex;
                padding: 0 24px;
                margin-top: 24px;

                .left {
                    display: flex;
                    flex-wrap: wrap;
                    flex: 1;
                }
            }
        }
    }
}
.wrapper {
    width: 100%;
    /deep/ .el-dialog__header {
        padding-top: 10px;
        box-shadow:0 1px 0 0 rgba(233,233,233,1);
        display: flex;
        align-items: center;
        background: #FAFAFA;
        border-radius: 4px 4px 0 0;
        .el-dialog__title {
            font-weight: 500;
            color: #333;
            font-size: 14px;
        }
        .el-icon-close:before {
            color: rgb(194,194,194);
            font-size: 17px;
            font-weight: bold;
        }
        .el-dialog__headerbtn {
            top: 14px;
        }
    }
    /deep/ .el-textarea__inner {
        resize: none;
    }
    .filter-wrap {
        padding: 0 24px;
        margin: 6px 0 24px;
        box-sizing: border-box;
        display: flex;
        justify-content: center;
        align-items: center;

        .header-new-box {
            display: flex;
            align-items: center;
            color: #606266;
            white-space: nowrap;

            &-title {
                margin-right: 2px;
                white-space: nowrap;
            }
        }

        .search-btn {
            border-radius: 0 4px 4px 0;
        }
        .input-with-select {
            width: 300px;
            /deep/ .el-input__inner {
                border-radius: 4px 0 0 4px;
                border-right: 0;
            }
        }
    }
    // 表格
    .data-table {
        ::-webkit-scrollbar {
            width: 6px;
            height: 6px;
        }
        // 操作按钮
        .option-btn-group {
            .el-button {
                margin-left: 0;
                margin-right: 10px;
            }
        }
    }
}
</style>

